/** to ActionEvent */
import java.awt.event.ActionEvent;  // $codepro.audit.disable noSpellingDictionary

/** to ActionListener */
import java.awt.event.ActionListener;

/**
 *  This is main class to manage entire our program.
 *  @author CodeTraders
 *  @version 1.0 11/18/2012
 */
public class Main_Present {
	
	/**
	 * create Player object
	 */
	private static final Player MYPLAYER = new Player();
	
	/**
	 *  create Universe object
	 */
	private static final Universe MYUNIVERSE = new Universe();
	
	/**
	 *  create  ship object
	 */
	private Ship myShip = new Ship();
	
	/**
	 *  create myView interface
	 */
	private final View myView;
	
	/**
	 * create myModle interface
	 */
	private Model myModel;
	
	/**
	 *  create MainGUI object
	 */
	private SpaceTraderMainGUI mainWindow;
	
	/**
	 * max ability points
	 */
	private static final int MAX_POINTS = 16;
	
	/**
	 * Constructor for the main
	 * 
	 * @param v the view
	 */
	public Main_Present(View v) {
		myView = v;
		myView.enableAccept(true);
        myView.registerAccept(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
            	MYPLAYER.setName(myView.getSkills().getName());
            	MYPLAYER.setPilot(myView.getSkills().getPilot());
            	MYPLAYER.setFighter(myView.getSkills().getFighter());
            	MYPLAYER.setTrader(myView.getSkills().getTrader());
            	MYPLAYER.setEngineer(myView.getSkills().getEngineer());
            	final String[] arg = {myView.getSkills().getName(),
            			String.valueOf(myView.getSkills().getPilot()),
            			String.valueOf(myView.getSkills().getFighter()),
            			String.valueOf(myView.getSkills().getTrader()),
            			String.valueOf(myView.getSkills().getEngineer()),
            			String.valueOf(myView.getDifficulty())};
                //System.out.println(java.util.Arrays.toString(arg));
                //System.out.println(Integer.parseInt(arg[1]) + Integer.parseInt(arg[2]) + Integer.parseInt(arg[3]) + Integer.parseInt(arg[4]));
                if (!arg[0].equals("")){
                	if(myView.getSkills().getEngineer() +
                			myView.getSkills().getFighter() +
                			myView.getSkills().getPilot() +
                			myView.getSkills().getTrader() == MAX_POINTS) {
                			mainWindow = new SpaceTraderMainGUI(arg, false);
                			mainWindow.show();
                			init_main(mainWindow);
                	}
                }
            }
        });
	}
	
	/**
	 * Creates the GUI
	 * 
	 * @param args args
	 */
	public static void main(String args[]) {
		final Main_Present main = new Main_Present(new SpaceTraderGUI());
		main.start();
	}
	
	/**
	 * starts the view
	 */
	public void start(){
		myView.show();
	}
	
	/**
	 * This method is for SpaceTraderMainGUI to initialize and to ready.
	 * To get universe, planet, ship and MarketPlace.
	 * @param m the model
	 */
	public void init_main(Model m){
		myModel = m;
        MYUNIVERSE.setPlanets(myModel.getPlanetList());  
        myShip = myModel.getShip();
        myModel.infoAccept(new ActionListener(){
        	 @Override
             public void actionPerformed(ActionEvent arg0) {
        		 checkinfo();
        	 }
        });
        myModel.storeAccept(new ActionListener(){
          	 @Override
             public void actionPerformed(ActionEvent arg0) {
          		 checkstore();
        	 }
        });
        myModel.mapAccept(new ActionListener(){
         	 @Override
            public void actionPerformed(ActionEvent arg0) {
         		 checkmap();
       	 }
       });
	}

	/**
	 * This is to restart this main game.
	 */
	public void restart(){
		myModel.restart();
	}
	
	/**
	 * To check information of map from SpaceTraderMainGUI
	 */
	public void checkmap(){
        System.out.println(myShip.getCurrentPlanet().getName());
        System.out.println(myShip.getCurrentPlanet().getHeight());
        System.out.println(myShip.getCurrentPlanet().getWidth());
        System.out.println(myShip.getCurrentPlanet().getResource());
        System.out.println(myShip.getCurrentPlanet().getTechLevel());
	}
	
	/**
	 * To check information of panel from SpaceTraderMainGUI
	 */
	public void checkinfo(){
		System.out.println(MYPLAYER.getName());
        System.out.println(myShip.getCurrentPlanet().getHeight());
        System.out.println(myShip.getCurrentPlanet().getName());
        System.out.println(myShip.getCurrentPlanet().getWidth());
        System.out.println(myShip.getCurrentPlanet().getResource());
        System.out.println(myShip.getCurrentPlanet().getTechLevel());
        System.out.println("Money " + myShip.getMoney());
        System.out.println("Type " + myShip.getShipType());
        System.out.println("Life " + myShip.getLife());
        System.out.println("Fuel " + myShip.getFuel());
	}
	
	/**
	 * To check information of store from SpaceTraderMainGUI
	 */
	public void checkstore(){
        final int[] goods = myShip.getGoods();
        for(int i =0; i < goods.length; i++){
        	System.out.print(goods[i] + " ");
        }
        System.out.println("");
        System.out.println("cargo space " + myShip.getCargoSpace());
        System.out.println("Money " + myShip.getMoney());
	}
	
	/**
	 * This is a toString method.
	 * @return tostring.
	 */
	 public String toString() {
		    return MYPLAYER.getName();
	 }
}